<?php
header("Content-Type:text/html; charset=utf-8");
include_once('../inc/init.php');
include_once('../inc/cache.php');
include_once('inc/adminfun.php');
if(!isAdmin()){
	exit("请登录");
}

$action=strFilter($_GET['action']);
switch($action){
	case "saveforum":
		try{
			$ordernums=$_POST['ordernum'];
			$forumnames=$_POST['forumname'];
			if(is_array($ordernums)) {
				foreach($ordernums as $id => $value) {
					$forum['ordernum'] = $value;
					$forum['forumname'] = $forumnames[$id];
					$db->row_update("forums",$forum,"id={$id}");
				}
			}
			writeForumsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;
	
	case "delforum":
		try{
			$fid=numFilter($_GET['fid']);
			if(empty($fid)){
				exit("参数错误");
			}
			$rows=$db->row_select("forums","pid={$fid}");
			if(!empty($rows)){
				exit("该版块存在下级版块，不能删除。");
			}
		
			//获取版块下面的帖子
			$topic_rows = $db->row_select("topics","fid={$fid}",0,"id");
			$topicids=array();
			$tidstr="";
			foreach($topic_rows as $row){
				array_push($topicids, $row['id']);
			}
			if(!empty($topicids)){
				$tidstr=implode("," ,$topicids);
			}
			
			//1.删除公告
			$db->row_delete("announces","fid={$fid}");
			
			//2.删除附件、帖子
			if(!empty($tidstr)){
				$attrows=$db->row_select("attachments","tid in ({$tidstr})");
				foreach($attrows as $row){
					$filepath="../uploadfile/attachment/".$row['filepath'];
					if(file_exists($filepath)){
						@unlink($filepath);
					}
					$db->row_delete("attachments","id={$row['id']}");
				}
				$db->row_delete("posts","tid in ({$tidstr})");
				$db->row_delete("topics","id in ({$tidstr})");
			}
			
			//删除online
			$db->row_delete("online","fid={$fid}");
			
			//删除reward
			if(!empty($tidstr)){
				$db->row_delete("reward","tid in ({$tidstr})");
			}
			//删除voteitems
			if(!empty($tidstr)){
				$db->row_delete("voteitems","tid in ({$tidstr})");
			}
			//删除votes
			if(!empty($tidstr)){
				$db->row_delete("votes","tid in ({$tidstr})");
			}
			//删除forums
			$db->row_delete("forums","id={$fid}");
			
			writeForumsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;
	
	case "saveadmins":
		try{
			$res='y';
			$id=numFilter($_GET['id']);
			if($id==0){
				echo("版块找不到！");
				exit();
			}
			$admins=$_POST['admins'];
			foreach ($admins as $key => $value) {
				$ordernum[$key] = $value['ordernum'];
			}
			array_multisort($ordernum, $admins);
			$adminsarr=array();
			foreach($admins as $key=>$value){
				$uname=$value['name'];
				if(numFilter($value['delid'])==0){
					if(empty($uname) || in_array($uname, $adminsarr)){
						continue;
					}
					//检查用户是否存在
					$userrow = $db->row_select_one("users","username='{$uname}'");
					if(empty($userrow)){
						echo("用户\"{$uname}\"不存在<br />");
						$res='';
					}else{
						array_push($adminsarr,$uname);
						//更新用户的groupid，设置为版主
						$userobj['groupid']=GROUP_FORUMADMIN;
						$db->row_update("users",$userobj,"username='{$uname}'");
					}
				}else{
					$rows= $db->row_select("forums","admins like '%|{$uname}|%' and id<>{$id}");
					if(empty($rows)){
						echo("emp");
						//更新用户的groupid，取消版主
						$userobj['groupid']=1;
						$db->row_update("users",$userobj,"username='{$uname}'");
					}
				}
			}
			$adminstr="|".implode("|",$adminsarr)."|";
			$forum['admins']=$adminstr;
			$db->row_update("forums",$forum,"id={$id}");
			writeForumsCache();
			echo($res);
		}catch(Exception $e){
			echo($e);
		}
	break;
	case "saveforumdetails":
		try{
			$doaction=strFilter($_POST['doaction']);
			$forum['forumname']=strFilter($_POST['forumname']);
			$forum['picurl']=strFilter($_POST['picurl']);
			$forum['forumcontent']=strFilter($_POST['forumcontent']);
			$forum['rules']=strFilter($_POST['rules']);
			$forum['metakeywords']=strFilter($_POST['metakeywords']);
			$forum['isneedverify']=numFilter($_POST['isneedverify']);
			$forum['ishidden']=numFilter($_POST['ishidden']);
			$forum['showtype']=numFilter($_POST['showtype']);
			$forum['pid']=numFilter($_POST['pid']);
			if($doaction=="edit"){
				$id=numFilter($_POST['id']);
				$db->row_update("forums",$forum,"id={$id}");
			}else{
				$forum['forumusers']='';
				$db->row_insert("forums",$forum);
			}
			writeForumsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "saveforumpopedom":
		try{
			$doaction=strFilter($_POST['doaction']);
			$forum['forumpass']=strFilter($_POST['forumpass']);
			$forum['forumusers']=strFilter($_POST['forumusers']);
			$forum['forumgroups']=strFilter($_POST['forumgroups']);
			if(substr($forum['forumgroups'],0,2)=='0,'){
				$forum['forumgroups']='0';
			}
			$id=numFilter($_POST['id']);
			$db->row_update("forums",$forum,"id={$id}");
			writeForumsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "mergeforum":
		require_once getCacheFilePath('forums.php');
		try{
			$fromfid=numFilter($_POST['fromfid']);
			$tofid=numFilter($_POST['tofid']);
			$childfid=$cache_forums[$fromfid]['childfid'];
			if(!empty($childfid)){
				echo("源版块存在下级版块，不能合并。请调整后再继续。");
				return;
			}			
			$post['fid']=$tofid;
			$db->row_update("posts",$post,"fid={$fromfid}");
			$topic['fid']=$tofid;
			$db->row_update("topics",$topic,"fid={$fromfid}");
			$db->row_delete("forums","id={$fromfid}");
			writeForumsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;

	
	default:
		echo"No Such Action";
	break;
}
?>
